<template>
  <div class="revocation">
    <el-table
      ref="multipleTable"
      :data="dataList"
      element-loading-text="加载中"
      fit
      border
      highlight-current-row
      @selection-change="handleSelectionChange"
      :header-cell-class-name="this.$verticaline"
      :height="borderHeight"
      :header-cell-style="tableHeightStyle"
      @header-dragend="changeTableWidth"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column
        label="订单号"
        prop="order_sn"
        :width="tableWidth[hash + 'order_sn'] || 180"
        show-overflow-tooltip
      />
      <el-table-column
        label="用户名称"
        prop="nickname"
        :width="tableWidth[hash + 'nickname']"
        show-overflow-tooltip
      >
        <template slot-scope="scope"
          >{{ scope.row.member ? scope.row.member.nickname : "" }}
          <span v-if="scope.row.zid_info"
            >【{{ scope.row.zid_info.nickname }}】</span
          ></template
        >
      </el-table-column>
      <el-table-column
        label="发货时间"
        prop="delivery_time"
        :width="tableWidth[hash + 'delivery_time']"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{
          scope.row.delivery_time
            ? dayjs(scope.row.delivery_time * 1000).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            : ""
        }}</template>
      </el-table-column>
      <el-table-column
        label="分拣状态"
        prop="status"
        :width="tableWidth[hash + 'status']"
        show-overflow-tooltip
      >
        <template slot-scope="scope"
          ><span :class="'order' + scope.row.order_list_status">
            {{ sort_status[scope.row.order_list_status] }}
          </span></template
        >
      </el-table-column>
      <el-table-column
        label="发货状态"
        prop="order_status"
        :width="tableWidth[hash + 'order_status']"
        show-overflow-tooltip
      >
        <template slot-scope="scope"
          ><span
            :class="
              scope.row.order_status == 2 || scope.row.order_status == 3
                ? 'success'
                : 'unpaid'
            "
          >
            {{
              scope.row.order_status == 2 || scope.row.order_status == 3
                ? "已发货"
                : "未发货"
            }}</span
          ></template
        >
      </el-table-column>
      <el-table-column
        label="配送路线"
        prop="line_id"
        :width="tableWidth[hash + 'line_id']"
        show-overflow-tooltip
      >
        <template slot-scope="scope"
          ><span>
            {{ line_info[scope.row.line_id] }}
          </span></template
        >
      </el-table-column>
    </el-table>
    <div class="revocation-btn">
      <el-button class="mainor_tea" type="primary" @click="batchRevocation">确认</el-button>
    </div>
    <!-- 批量撤回 -->
    <el-dialog
      :title="'已选择' + revocationList.length + '个订单，撤回订单中！'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="removeFormVisible"
      class="main_dialog dialog450"
      append-to-body
    >
      <el-progress
        :text-inside="true"
        :stroke-width="15"
        :percentage="+(revocationData.length / revocationList.length * 100).toFixed(0)"
        status="success"
        color="#E47C02"
        style="width: 100%"
      ></el-progress>
      <div class="progress_list">
        <div class="progress_list_wrap">
          <template v-for="(percent_list, index) in revocationTypeData">
            <template>
              <p v-if="percent_list.type == 1" :key="index">
                订单id:{{ `${percent_list.order_id}，${percent_list.msg}` }}
              </p>
              <p v-else class="progress_list_red" :key="index">
                订单id:{{ `${percent_list.order_id}，${percent_list.msg}` }}
              </p>
            </template>
          </template>
        </div>
        <p v-if="(revocationData.length / revocationList.length * 100).toFixed(0) == 100" class="progress_list_su">
          撤回已经完成请关闭弹框!
        </p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import dayjs from "dayjs";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import {
  return_order,
} from "@/api/warehouse/warehouseDelivery";
export default {
  name: 'revocation',
  mixins: [mixins],
  props: {
    dataList: {
      type: Array,
      default: () => []
    },
    sort_status: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      selectArr: [],  //表格选中数据
      line_info: {},
      dayjs,
      revocationTypeData: [], //  批量撤回数据，用于内弹窗来展示当前撤回进度
      revocationList: [], //  总撤回数据
      removeFormVisible: false,
      revocationData: []  //  当前已撤回数据
    }
  },
  methods: {
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
    },
    // 批量撤回
    async batchRevocation () {
      if (this.selectArr.length != 0) {
        this.revocationTypeData = []
        let ids = this.selectArr.map((item) => item.id);
        this.revocationList = ids
        this.revocationData = []
        this.removeFormVisible = true
        for (const id of ids) {
          await this.revocationById(id)
        }
      } else {
        this.$message.error("请勾选订单");
      }
    },
    //  单个撤回
    async revocationById(id) {
      let data = {
        id
      };
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, data)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        data
      );
      await return_order(query).then((res) => {
        this.revocationData.push(id)
        if (res.errcode == 0) {
          this.revocationTypeData.push({
            num: 100,
            type: 1,
            order_id: id,
            msg: res.errmsg,
          })
        } else if (res.errcode == 40009) {
          this.revocationTypeData.push({
            num: 100,
            type: 2,
            order_id: id,
            msg: res.errmsg,
          })
        }
      });
    },
    close () {
      this.removeFormVisible = false
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.revocation .el-checkbox {
  margin-bottom: 0px !important;
}
.revocation-btn {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.progress_list {
  margin-top: 10px;
  .progress_list_wrap {
    max-height: 300px;
    overflow-y: auto;
    margin-bottom: 10px;
  }
  p {
    height: 24px;
    line-height: 24px;
  }
  .progress_list_red {
    color: red;
  }
  .progress_list_su {
    color: #03a98e;
  }
}
</style>